#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : BaseEditor(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    table=REF_TABLE;
    colTable=REF_COLUMN;
    addFlag=ADD_TABLE;

    model=new QSqlQueryModel(this);
    ui->tableView->setModel(model);
    createMenu();
    updateKeyMap();
    updateView();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::generateMenu()
{
    auto menuAdd=menu->addMenu("添加 (F1)");
    menuAdd->addAction("添加表... ",this,&Widget::addTable);
    menuAdd->addAction("添加列... ",this,&Widget::addColumn);
    menu->addSeparator();
    menu->addAction("更新视图",this,&Widget::updateView);
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_F1:
        addItem();
        break;
    }
}

void Widget::addItem()
{
    if(addFlag == ADD_TABLE)
        addTable();
    else
        addColumn();
}

void Widget::addTable()
{
    AddTable item;
    if(item.exec() == QDialog::Accepted)
        updateView();
}

void Widget::addColumn()
{
    if(selectTableName.isEmpty() || !idMap.contains(selectTableName))
        return;

    int id=idMap.value(selectTableName);
    AddColumn item(this,selectTableName,id);
    if(item.exec() == QDialog::Accepted)
        updateFilterView();
}

void Widget::updateView()
{
    addFlag=ADD_TABLE;
    QString sql=QString("select table_name,table_label,column_name,column_label"
                        " from %1 LEFT JOIN %2 ON %1.id=%2.table_id")
            .arg(table)
            .arg(colTable);

    model->setQuery(sql);
    model->setHeaderData(0, Qt::Horizontal, QObject::tr("tableName"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("tableLabel"));
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("columnName"));
    model->setHeaderData(3, Qt::Horizontal, QObject::tr("columnLabel"));
    ui->tableView->resizeColumnsToContents();
}

void Widget::updateKeyMap()
{
    if(!keyMap.isEmpty())
        keyMap.clear();

    if(!idMap.isEmpty())
        idMap.clear();

    QSqlQuery query;
    QString sql=QString("select id,table_label,table_name from %1")
            .arg(table);
    query.exec(sql);
    while (query.next()) {
        auto id=query.value(0).toInt();
        auto label=query.value(1).toString();
        auto name=query.value(2).toString();
        keyMap.insert(label,name);
        idMap.insert(name,id);
    }

    if(!keyMap.isEmpty())
    {
        ui->tableLabels->addItems(keyMap.keys());
        ui->tableLabels->setCurrentIndex(-1);
        ui->tableName->clear();
    }
}

void Widget::on_tableLabels_currentTextChanged(const QString &key)
{
    if(keyMap.contains(key))
        ui->tableName->setText(keyMap.value(key));
}

void Widget::updateFilterView()
{
    addFlag=ADD_COLUMN;
    selectTableName=ui->tableName->text().trimmed();
    if(selectTableName.isEmpty())
        return;

    QString sql=QString("select table_label,column_name,column_label"
                        " from %1 JOIN %2 ON %1.id=%2.table_id"
                        " WHERE table_name='%3'")
            .arg(table)
            .arg(colTable)
            .arg(selectTableName);

    model->setQuery(sql);
    model->setHeaderData(0, Qt::Horizontal, QObject::tr("tableLabel"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("columnName"));
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("columnLabel"));
    ui->tableView->resizeColumnsToContents();
}

void Widget::on_btn_filter_clicked()
{
    updateFilterView();
}
